<template>
  <div v-if="visible">
    <el-dialog :visible.sync="visible"
               title="物联信息"
               :modal="false"
               class="user-dialog add-or-updata"
               width="45%"
               :before-close="closeDialog"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form :model="dataForm"
               v-loading="dataListLoading"
               :element-loading-text="$t('loading')"
               ref="dataForm"
               label-position="left"
               @keyup.enter.native="dataFormSubmitHandle()"
               label-width="80px">
        <el-form-item label="用户名"
                      prop="username"
                      :rules="required()">
          <el-input v-model.trim="dataForm.username"
                    maxlength="20"
                    show-word-limit
                    placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      prop="password"
                      :rules="required()">
          <el-input v-model.trim="dataForm.password"
                    maxlength="20"
                    show-word-limit
                    placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item v-if="type === 1"
                      prop="versionNo"
                      label="版本号"
                      :rules="required()">
          <el-select v-model="dataForm.versionNo"
                     clearable
                     style="width:100%"
                     placeholder="请选择版本号">
            <el-option v-for="item in versionList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="IP地址"
                      prop="ip"
                      :rules="required()">
          <el-input v-model.trim="dataForm.ip"
                    maxlength="20"
                    show-word-limit
                    placeholder="请输入IP地址"></el-input>
        </el-form-item>
        <el-form-item label="端口号"
                      prop="port"
                      :rules="required()">
          <el-input v-model.trim="dataForm.port"
                    maxlength="20"
                    show-word-limit
                    onkeyup="value=value.replace(/[^\d]/g,'')"
                    placeholder="请输入端口号"></el-input>
        </el-form-item>
        <el-form-item v-if="type === 1"
                      label="通道号"
                      prop="channelNo"
                      :rules="required()">
          <el-input v-model.trim="dataForm.channelNo"
                    maxlength="20"
                    show-word-limit
                    onkeyup="value=value.replace(/[^\d]/g,'')"
                    placeholder="请输入通道号"></el-input>
        </el-form-item>
        <el-form-item v-if="type === 1"
                      label="连接方式"
                      prop="connectType"
                      :rules="required()">
          <el-select v-model="dataForm.connectType"
                     clearable
                     style="width:100%"
                     placeholder="请选择设备类型">
            <el-option v-for="item in connectTypeList"
                       :key="item.id"
                       :label="item.name"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template slot="footer">
        <el-button @click="closeDialog()">{{ $t("cancel") }}</el-button>
        <el-button type="primary"
                   :loading="dataListLoading"
                   @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import minFormValidate from '@/mixins/form-validate'
import debounce from 'lodash/debounce'
export default {
  mixins: [mixinViewModule, minFormValidate],
  data () {
    return {
      mixinViewModuleOptions: {
        deleteImgURL: '/system/oss/delete',
        getDataListURL: '', // 数据列表接口，API地址
        getDataListIsPage: true, // 数据列表接口，是否需要分页？
        addURL: 'tb/device/insertDevice', // 添加或修改接口，API地址
        updateURL: 'tb/device/updateDevice', // 添加或修改接口，API地址
        getDataInfoURL: '', // 数据详情，API地址
        deleteURL: '', // 删除接口，API地址
        deleteIsBatch: true, // 删除接口，是否需要批量？
        exportURL: '' // 导出接口，API地址
      },
      visible: false,
      dataForm: {
        username: '',
        password: '',
        ip: '',
        connectType: 1,
        port: '554',
        channelNo: '',
        versionNo: ''
      },
      type: null,
      versionList: [{ id: '0', name: '大华' },
        { id: '1', name: '海康旧版本' },
        { id: '2', name: '海康新版本' }],
      connectTypeList: [{ // 连接方式
        id: 0, name: '直连'
      }, {
        id: 1, name: 'NVR'
      }],
      id: '',
      deviceId: ''
    }
  },
  mounted () {
  },
  methods: {
    /**
     * @description:
     * @example:
     * @param {object} data
     * @param {number} type 1:摄像头 2:音频
     * @return {*}
     */
    init (data, type) {
      if (data) {
        this.id = data.id
        this.dataForm.creator = data.creator
        this.dataForm.createDate = data.createDate
      }
      if (type) {
        this.type = type
      }
      this.$nextTick(() => {
        this.$http.get('/tb/device/getIOTInformation?id=' + data.id).then(({ data: res }) => {
          if (res.code !== 0) {
            return
          }
          if (res.data) {
            this.dataForm = res.data
          } else {
            this.dataForm.username = ''
            this.dataForm.password = ''
            this.dataForm.ip = ''
            this.dataForm.connectType = 1
            this.dataForm.port = '554'
            this.dataForm.channelNo = ''
            this.dataForm.versionNo = ''
          }
        })
      })
      this.visible = true
    },
    closeDialog () {
      this.type = null
      this.id = ''
      this.dataForm.ip = ''
      this.dataForm.port = ''
      this.dataForm.username = ''
      this.dataForm.password = ''
      this.dataForm.connectType = 1
      this.dataForm.channelNo = ''
      this.dataForm.versionNo = ''
      this.$emit('callBack')
      this.visible = false
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs.dataForm.validate((valid) => {
          if (valid) {
            let obj = {}
            obj.data = {}
            obj.data.id = this.id
            if (this.type === 1) { // 1:摄像头 2:音频
              obj.data.cameraInfo = {}
              obj.data.cameraInfo.ip = this.dataForm.ip
              obj.data.cameraInfo.port = Number(this.dataForm.port)
              obj.data.cameraInfo.username = this.dataForm.username
              obj.data.cameraInfo.password = this.dataForm.password
              obj.data.cameraInfo.creator = this.dataForm.creator
              obj.data.cameraInfo.createDate = this.dataForm.createDate
              obj.data.cameraInfo.connectType = this.dataForm.connectType
              obj.data.cameraInfo.channelNo = Number(this.dataForm.channelNo)
              obj.data.cameraInfo.versionNo = this.dataForm.versionNo
            } else if (this.type === 2) { // 1:摄像头 2:音频
              obj.data.audioInfo = {}
              obj.data.audioInfo.ip = this.dataForm.ip
              obj.data.audioInfo.port = Number(this.dataForm.port)
              obj.data.audioInfo.username = this.dataForm.username
              obj.data.audioInfo.password = this.dataForm.password
              obj.data.audioInfo.creator = this.dataForm.creator
              obj.data.audioInfo.createDate = this.dataForm.createDate
            }
            this.$http.put('tb/device/updateIOTInformation', obj).then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg)
              }
              this.$message.success('保存成功')
              this.closeDialog()
            })
          }
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>

<style lang="scss" scoped>
</style>
